package com.inphase.myfood.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inphase.myfood.dao.base.BaseController;
import com.inphase.myfood.model.Dash;
import com.inphase.myfood.model.Detailinfo;
import com.inphase.myfood.model.Food;
import com.inphase.myfood.model.Picture;
import com.inphase.myfood.service.DashService;
import com.inphase.myfood.service.DetailInfoService;

@Controller
@RequestMapping("/dash")
public class DashController extends BaseController{

	/**
	 * @author Tangxuebing
	 * @return 返回所有餐厅的信息
	 */
	@RequestMapping("/list.do")
	@ResponseBody
	public Map<String, Object> getDashList() {
		Map<String, Object> finalMap = new HashMap<String, Object>();
		List<Dash> list = dashService.getListObjects();
		List<Map> jsonList = new ArrayList<Map>();
		for (Dash dash : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dashName", dash.getDashName());
			Detailinfo detailinfo = detailInfoService.getObjectByPK(dash
					.getDashPoi());
			map.put("dashPic", detailinfo.getPicture());
			map.put("price", detailinfo.getPrice());
			map.put("tag", detailinfo.getTag());
			map.put("overall_rating", detailinfo.getOverallRating());
			jsonList.add(map);
		}
		finalMap.put("list", jsonList);
		return finalMap;
	}

	/**
	 * @author Tangxuebing
	 * @return 返回一个餐厅的所有菜品信息
	 */
	@RequestMapping("/getfoods.do")
	@ResponseBody
	public Map<String, Object> getFoodsFromDash(
			@RequestParam(required = true, value = "dashId") String dashId) {
		Map<String, Object> finalMap = new HashMap<String, Object>();
		List<Map> jsonList = new ArrayList<Map>();
		Dash dash = dashService.getObjectByPK(new Long(dashId));
		for (Iterator<Food> iterator = dash.getFoods().iterator(); iterator
				.hasNext();) {
			Map<String, Object> map = new HashMap<String, Object>();
			Food food = iterator.next();
			map.put("foodName", food.getFoodName());
			for (Iterator<Picture> iterator2 = food.getPictures().iterator(); iterator2
					.hasNext();) {
				Picture picture = iterator2.next();
				map.put("foodPicture", picture.getPicPath());
			}
			map.put("foodIntro", food.getFoodIntro());
			jsonList.add(map);
		}
		finalMap.put("list", jsonList);
		return finalMap;
	}
}
